package com.naodong.shenluntiku.mvp.view.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.di.component.DaggerAnalysisDetailComponent;
import com.naodong.shenluntiku.di.module.AnalysisDetailModule;
import com.naodong.shenluntiku.di.module.CollectOperateModule;
import com.naodong.shenluntiku.mvp.contract.AnalysisDetailContract;
import com.naodong.shenluntiku.mvp.contract.CollectOperateContract;
import com.naodong.shenluntiku.mvp.model.bean.EventBusTag;
import com.naodong.shenluntiku.mvp.model.data.manager.BoxManager;
import com.naodong.shenluntiku.mvp.presenter.AnalysisDetailPresenter;
import com.naodong.shenluntiku.mvp.presenter.CollectOperatePresenter;
import com.naodong.shenluntiku.mvp.view.webView.SimpleTKWebViewListener;
import com.naodong.shenluntiku.mvp.view.webView.TKWebViewFragment;
import com.naodong.shenluntiku.mvp.view.widget.TopTipViewManager;
import com.yatatsu.autobundle.AutoBundleField;

import javax.inject.Inject;

import butterknife.BindView;
import me.shingohu.man.base.BaseMVPActivity;
import me.shingohu.man.di.component.AppComponent;
import me.shingohu.man.intergation.EventManager;
import me.shingohu.man.model.bean.SimpleEvent;


/**
 * 通过Template生成对应页面的MVP和Dagger代码,请注意输入框中输入的名字必须相同
 * 由于每个项目包结构都不一定相同,所以每生成一个文件需要自己导入import包名,可以在设置中设置自动导入包名
 * 请在对应包下按以下顺序生成对应代码,Contract->Model->Presenter->Activity->Module->Component
 * 因为生成Activity时,Module和Component还没生成,但是Activity中有它们的引用,所以会报错,但是不用理会
 * 继续将Module和Component生成完后,编译一下项目再回到Activity,按提示修改一个方法名即可
 * 如果想生成Fragment的相关文件,则将上面构建顺序中的Activity换为Fragment,并将Component中inject方法的参数改为此Fragment
 */

/**
 * Created by hujie on 17/5/12.
 */

public class AnalysisDetailActivity extends BaseMVPActivity<AnalysisDetailPresenter> implements AnalysisDetailContract.View, CollectOperateContract.View {

    @Inject
    CollectOperatePresenter collectPresenter;


    TKWebViewFragment fragment;

    @BindView(R.id.menuView)
    LinearLayout menuView;
    ImageView collectView;

    @AutoBundleField
    String url;
    @AutoBundleField
    int id;
    @AutoBundleField
    int type;

    @AutoBundleField(required = false)
    String tip;

    TopTipViewManager topTipViewManager;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerAnalysisDetailComponent
                .builder()
                .appComponent(appComponent)
                .collectOperateModule(new CollectOperateModule(this))
                .analysisDetailModule(new AnalysisDetailModule(this)) //请将AnalysisDetailModule()第一个首字母改为小写
                .build()
                .inject(this);
    }

    @Override
    protected void onCreateInit(Bundle savedInstanceState) {

        setTitle("查看解析");
        addRightCollectView();
        fragment = TKWebViewFragment.getInstance();
        fragment.setTkWebViewListener(new SimpleTKWebViewListener() {
            @Override
            public void setReceiveTitle(String title) {
                setTitle(title);
            }

            @Override
            public void onPageFinished() {
                if (!TextUtils.isEmpty(tip)) {
                    if (topTipViewManager == null) {
                        topTipViewManager = new TopTipViewManager((ViewGroup) findViewById(R.id.container));
                    }
                    topTipViewManager.showTipView(tip);
                    tip = null;
                }
            }
        });
        fragment.setUrl(url);
        loadRootFragment(R.id.container, fragment);
    }

    @Override
    protected int layoutId() {
        return R.layout.a_analysisdetail;
    }

    @Override
    public void showMessage(String msg) {

    }


    @Override
    protected void gc() {
        if (topTipViewManager != null) {
            topTipViewManager.destory();
        }
        super.gc();
    }

    @Override
    public void onRequesrStart() {
        progressHudUtil.showProgressHud();
    }

    @Override
    public void onRequestFinish() {
        progressHudUtil.dismissProgressHud();
    }


    @Override
    public void onCollectSuccess(int resourceId, boolean status) {
        collectView.setSelected(status);
    }


    private void addRightCollectView() {
        menuView.setVisibility(View.VISIBLE);
        collectView = (ImageView) LayoutInflater.from(this).inflate(R.layout.menu_image, null);
        menuView.addView(collectView);
        collectView.setBackgroundResource(R.drawable.collect_selector);
        collectView.setSelected(BoxManager.getInstance().queryBoxCollectStatusByIdAndType(id, type));
        menuView.setOnClickListener(v -> {
            collectPresenter.requestPostCollect(id, type, BoxManager.getInstance().queryBoxCollectStatusByIdAndType(id, type));
        });
    }

    @Override
    protected boolean userAutoBindBundle() {
        return true;
    }

    @Override
    public void finish() {
        checkCancleCollect();
        super.finish();
    }


    public void checkCancleCollect() {
        if (!BoxManager.getInstance().queryBoxCollectStatusByIdAndType(id, type)) {
            SimpleEvent event = new SimpleEvent(EventBusTag.NOTIFY_CANCLE_COLLECT, id + "", type);
            EventManager.getInstance().post(event);

        }
    }
}